// 定义 HomeInitModel 类，实现可序列化
class HomeInitModel {
  int code;
  String msg;
  HomeInitClassModel? data;

  HomeInitModel({
    required this.code,
    required this.msg,
    this.data,
  });

  // 从 JSON 数据创建 HomeInitModel 实例
  factory HomeInitModel.fromJson(Map<String, dynamic> json) {
    return HomeInitModel(
      code: json['code'] as int,
      msg: json['msg'] as String,
      data: json['data'] != null
          ? HomeInitClassModel.fromJson(json['data'] as Map<String, dynamic>)
          : null,
    );
  }
}

// 定义 HomeInitClassModel 类，包含各种列表数据
class HomeInitClassModel {
  int currency;
  String currencyCode;
  List<CurrencyListModel> currencyList;
  List<HotSearchListModel> hotSearchList;
  List<HotGoodsListModel> hotGoodsList;

  HomeInitClassModel({
    required this.currency,
    required this.currencyCode,
    required this.currencyList,
    required this.hotSearchList,
    required this.hotGoodsList,
  });

  // 从 JSON 数据创建 HomeInitClassModel 实例
  factory HomeInitClassModel.fromJson(Map<String, dynamic> json) {
    return HomeInitClassModel(
      currency: json['currency'] as int,
      currencyCode: json['currencyCode'] as String,
      currencyList: (json['currencyList'] as List<dynamic>?)
              ?.map((e) => CurrencyListModel.fromJson(e as Map<String, dynamic>))
              .toList() ??
          [],
      hotSearchList: (json['hotSearchList'] as List<dynamic>?)
              ?.map((e) => HotSearchListModel.fromJson(e as Map<String, dynamic>))
              .toList() ??
          [],
      hotGoodsList: (json['hotGoodsList'] as List<dynamic>?)
              ?.map((e) => HotGoodsListModel.fromJson(e as Map<String, dynamic>))
              .toList() ??
          [],
    );
  }
}

// 定义 CurrencyListModel 类，包含货币列表信息
class CurrencyListModel {
  int currencyId;
  String name;
  String code;

  CurrencyListModel({
    this.currencyId  = 23,  // 默认值 23 
    required this.name,
    required this.code,
  });

  // 从 JSON 数据创建 CurrencyListModel 实例
  factory CurrencyListModel.fromJson(Map<String, dynamic> json) {
    return CurrencyListModel(
      currencyId: json['currencyId'] as int,
      name: json['name'] as String,
      code: json['code'] as String,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'currencyId': currencyId,
      'name': name,
      'code': code,
    };
  }
}

// 定义 HotSearchListModel 类，包含热门搜索列表信息
class HotSearchListModel {
  String name;

  HotSearchListModel({
    required this.name,
  });

  // 从 JSON 数据创建 HotSearchListModel 实例
  factory HotSearchListModel.fromJson(Map<String, dynamic> json) {
    return HotSearchListModel(
      name: json['name'] as String,
    );
  }
}

// 定义 HotGoodsListModel 类，包含热门商品列表信息
class HotGoodsListModel {
  int id;
  String name;
  String pic;

  HotGoodsListModel({
    required this.id,
    required this.name,
    required this.pic,
  });

  factory HotGoodsListModel.fromJson(Map<String, dynamic> json) {
    final rawId = json['id'];
    final parsedId = rawId is int
        ? rawId
        : int.tryParse(rawId.toString()) ?? 0;

    return HotGoodsListModel(
      id: parsedId,
      name: json['name'] as String,
      pic: json['pic'] as String,
    );
  }
}


// 定义 HotRecommendListModel 类，包含热门推荐列表信息
class HotRecommendListModel {
  int id;
  String name;
  String pic;

  HotRecommendListModel({
    required this.id,
    required this.name,
    required this.pic,
  });

  // 从 JSON 数据创建 HotRecommendListModel 实例
  factory HotRecommendListModel.fromJson(Map<String, dynamic> json) {
    return HotRecommendListModel(
      id: json['id'] as int,
      name: json['name'] as String,
      pic: json['pic'] as String,
    );
  }
}